package net.yeah.mickey_zhouyou.database.util.new_instance_policy;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import net.yeah.mickey_zhouyou.database.domain.BaseEntity;
import net.yeah.mickey_zhouyou.database.util.NewInstancePolicy;

public class NewEntityPolicy implements NewInstancePolicy<BaseEntity> {

	private static final Map<String, BaseEntity> prototypeCache = new ConcurrentHashMap<String, BaseEntity>();

	@Override
	public BaseEntity newInstance(String className) {
		BaseEntity entity = prototypeCache.get(className);
		if (entity == null)
			try {
				entity = (BaseEntity) Class.forName(className).newInstance();
				prototypeCache.put(className, entity);
			} catch (Exception e) {
				e.printStackTrace();
			}
		return (BaseEntity) entity.clone();
	}

	private NewEntityPolicy() {
	}

	private static final NewEntityPolicy policy = new NewEntityPolicy();

	public static NewEntityPolicy getPolicy() {
		return policy;
	}

}
